package com.livery.app.model.tables;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.livery.data.Client;

public class ClientModel extends AbstractTableModel{

	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	private List<Client> listClient;
	private String[] columnNames = {
			"Imię",
			"Nazwisko",
			"Miejscowość",
			"Ulica",
			"Kod pocztowy",
			"Telefon",
			"E-mail",
			"Nr dowodu",
			"Pesel",
			"Zniżka - %"
	};
	
	public ClientModel(List<Client> listClient){
		this.listClient = listClient;
	}
	
	public List<Client> getListClient() {
		return listClient;
	}

	public void setListClient(List<Client> listClient) {
		this.listClient = listClient;
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return 10;
	}
	@Override
	public String getColumnName( int columnIndex )
	{
		return columnNames[ columnIndex ];
	}
	
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return listClient.size();
	}

	@Override
	public Object getValueAt( int row, int col )
	{
		switch(col)
		{
			case 0 : return this.listClient.get( row ).getFirstName();
			case 1 : return this.listClient.get( row ).getLastName();
			case 2 : return this.listClient.get(row).getCity();
			case 3 : return this.listClient.get(row).getStreet();
			case 4 : return this.listClient.get(row).getPostalCode();
			case 5 : return this.listClient.get(row).getPhoneNumber();
			case 6 : return this.listClient.get(row).getMail();
			case 7 : return this.listClient.get(row).getDocumentID();
			case 8 : return this.listClient.get(row).getPIN();
			case 9 : return this.listClient.get(row).getDiscount();
			default : throw new RuntimeException("Nie znaleziono kolumny");
		}
	}
	
	public Client getClientAt(int row){
		return listClient.get(row);
	}
	
	public void addRow(Client client){
		this.listClient.add(client);
		TableModelEvent event = new TableModelEvent( this, this.listClient.size() - 1, this.listClient.size() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}
	public void removeRow(Client client){
		int index = listClient.indexOf(client);
		listClient.remove(client);
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}
	@Override
	public void addTableModelListener( TableModelListener pTableModelListener )
	{
		if( listeners.contains( pTableModelListener ) )
		{
			return;
		}

		listeners.add( pTableModelListener );
	}

	@Override
	public void removeTableModelListener(TableModelListener l)
	{
		listeners.remove(l);
	}
}
